package com.emm.yixun.jobs;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.emm.yixun.common.model.BaseCodeType;
import com.emm.yixun.common.model.BaseCodeValue;
import com.emm.yixun.common.model.dto.BaseCodeTypeDto;
import com.emm.yixun.common.model.dto.BaseCodeValueDto;
import com.emm.yixun.common.response.ResponseList;
import com.emm.yixun.jobs.common.email.MailConfig;
import com.emm.yixun.jobs.common.email.MailUtil;
import com.emm.yixun.jobs.common.utils.DiskFreeUtil;
import com.emm.yixun.system.service.BaseCodeTypeServiceFacade;
import com.emm.yixun.system.service.BaseCodeValueServiceFacade;

@Component
public class DiskFreeJob {
	
	private static Logger logger = LoggerFactory.getLogger(DiskFreeJob.class);
	
	@Autowired
	private BaseCodeTypeServiceFacade baseCodeTypeServiceFacade;
	@Autowired
	private BaseCodeValueServiceFacade baseCodeValueServiceFacade;
	
	/**
	 * 发送磁盘已满的预警信息
	 * 每小时执行一次，磁盘使用率超过90%，则发送邮件
	 */
	@Scheduled(cron = "0 0 */1 * * ?")
	public void sendDiskFree() {
		logger.info("磁盘预警开始执行==>");
		//获取磁盘信息
		Map<String, String> map = DiskFreeUtil.diskFree();
		//可用空间
		String freeSpace = map.get("freeSpace");
		//已用空间
		String usableSpace = map.get("usableSpace");
		//全部空间
		String totalSpace = map.get("totalSpace");
		//已用百分比
		String percent = map.get("percent");
		//是否已超过安全值 （安全值=90%, 0 否 1是）
		String isExceed = map.get("isExceed");
		
		//未超过安全值时，退出
		if(!"1".equals(isExceed)) {
			return;
		}
		
		StringBuffer sb = new StringBuffer();
		sb.append("硬盘使用率已超过90%，当前为：");
		sb.append(percent);
		sb.append("<br/>硬盘总空间：");
		sb.append(totalSpace);
		sb.append("<br/>已用空间：");
		sb.append(usableSpace);
		sb.append("<br/>剩余空间：");
		sb.append(freeSpace);
		//超过安全值时，发送预警邮件
		MailConfig config = MailUtil.setConfig("mail.properties");
		//获取收件人信息
		String sendMailAddresses = sendMailAddresses();
		//发送邮件
		MailUtil.sendProcess(config, sendMailAddresses, null, null, "M2 磁盘使用率已超90%", sb.toString(), null);
	}
	
	/**
	 * 获取收件人信息
	 * @return
	 */
	private String sendMailAddresses() {
		String sendMailAddresses = "";
		BaseCodeTypeDto bctDto = new BaseCodeTypeDto();
		bctDto.setCodeType("EM");
		ResponseList<BaseCodeType> bctRes = baseCodeTypeServiceFacade.findByDto(bctDto);
		if (bctRes.isSuccess()) {
			List<BaseCodeType> bctList = bctRes.getEntity();
			if (null != bctList && bctList.size() > 0) {
				BaseCodeType bct = bctList.get(0);
				long bctId = bct.getId();
				BaseCodeValueDto bcvDto = new BaseCodeValueDto();
				bcvDto.setTypeId(bctId);
				ResponseList<BaseCodeValue> bcvRes = baseCodeValueServiceFacade.findByDto(bcvDto);
				if (bctRes.isSuccess()) {
					List<BaseCodeValue> bcvList = bcvRes.getEntity();
					if (null != bcvList && bcvList.size() > 0) {
						for (BaseCodeValue bcv : bcvList) {
							String codeName = bcv.getCodeName();
							String codeValue = bcv.getCodeValue();
							if ("是否发送邮件".equals(codeName)) {
								//是否发送邮件为否时，返回空值
								if (!"1".equals(codeValue)) {
									return "";
								}
							}
							if ("接收人邮件".equals(codeName) && !StringUtils.isBlank(codeValue)) {
								sendMailAddresses += codeValue + ";";
							}
						}
					}
				}
			}
		}
		
		return sendMailAddresses;
	}
}
